<template>
	<view class="cu-card article bg-white">
		<view class="cu-item" v-for="(item, index) in newList" :key='index' @tap="goPage(item.id)">
			<view class="title">{{htmlEncode(item.title)}}</view>
			<view class="cu-content bg-white">
				<view class="desc bg-white">
					<p class="text-content">{{htmlEncode(item.description)}}</p>
					<view class="tag" v-if='isHome'>{{item.typeid === 2? '平台公告':item.typeid === 1? "项目动态" : "行业资讯" }}</view>
					<view class="time" v-else><span>好融艺</span>{{item.create_time}}</view>
				</view>
				<image :src="imgUrl + item.litpic" mode="aspectFit"></image>
			</view>
		</view>
	</view>
	</view>

</template>

<script>
	import moment from "moment";
	export default {
		name: "index",
		props: {
			newList: {
				type: Array
			},
			isHome: {
				type: Boolean,
				default: true
			}
		},
		data() {
			let that = this;
			return {
				imgUrl: that.$mConfig.assetsPath,
				timer: null,
				endTime: null,
				nowDate: "",
			};
		},
		methods: {
			moment,
			goPage(id) {
				this.$mRouter.push({
					route: this.$mRoutesConfig.infoDetail,
					query: {
						id: id
					}
				})
			},
			htmlEncode(text) {
				if (text) {
					var arrEntities = {
						lt: "<",
						gt: ">",
						nbsp: " ",
						amp: "&",
						quot: '"'
					};
					return text.replace(/&(lt|gt|nbsp|amp|quot);/gi, function(all, t) {
						return arrEntities[t];
					});
				} else {
					return "";
				}
			},
			formatStatusClass(status) {
				switch (status) {
					case 1:
						return "text-ready";
					case 2:
						return "hry-current";
					case 3:
						return "hry-success";
					case 4:
						return "hry-success";
					case 5:
						return "hry-success";
					case 6:
						return "hry-success";
				}
			},
			formatImg(thumb) {
				if (thumb.indexOf("http") !== -1) {
					return thumb;
				} else {
					return this.imgUrl + thumb;
				}
			},
			formatTimer() {
				if (this.endTime) {
					this.nowDate = this.moment().unix();
					if (this.nowDate < this.endTime) {
						let mixinTime = this.endTime - this.nowDate;
						var dd = Math.floor(mixinTime / 60 / 60 / 24);
						var hh = Math.floor((mixinTime / 60 / 60) % 24);
						var mm = Math.floor((mixinTime / 60) % 60);
						var ss = Math.floor(mixinTime % 60);
						return "剩余时间：" + dd + "天" + hh + "小时" + mm + "分" + ss + "秒";
					} else {
						return "";
					}
				} else {
					return "";
				}
			},
			formatStatus(status) {
				switch (status) {
					case 1:
						return "预热中";
					case 2:
						return "认投中";
					case 3:
						return "已成功";
					case 4:
						return "已成功";
					case 5:
						return "已成功";
					case 6:
						return "已成功";
				}
			},
		},
		watch: {
			cardInfo: {
				handler(val) {
					if (val) {
						if (val.status === 2) {
							this.endTime = JSON.parse(JSON.stringify(val.end_time));
							if (this.timer) {
								clearInterval(this.timer);
							}
							this.timer = setInterval(() => {
								this.formatTimer();
							}, 1000);
						}
					}
				},
				deep: true,
				immediate: true
			}
		},
		beforeDestroy() {
			clearInterval(this.timer);
			this.timer = null;
		}
	};
</script>

<style lang="scss" scoped>
	.cu-card.article {
		.cu-item {
			border-bottom: 24rpx solid #F7F7F7;

			.title {
				font-size: 30rpx;
				font-family: PingFang SC;
				font-weight: bold;
				color: rgba(50, 50, 50, 1);
				margin-bottom: 20rpx;
				-webkit-line-clamp: 1;
			}

			.cu-content {
				.text-content {
					font-size: 26rpx;
					font-family: PingFang SC;
					font-weight: 400;
					color: rgba(51, 51, 51, 1);
					margin: 0 0 17rpx;
					line-height: 33rpx;
					-webkit-line-clamp: 3;
				}

				image {
					width: 300rpx;
					height: 141rpx;
					border-radius: 6rpx;
				}

				.time {
					font-size: 20rpx;
					font-family: PingFang SC;
					color: rgba(178, 178, 178, 1);

					span {
						margin-right: 30rpx;
					}
				}

				.tag {
					font-size: 24rpx;
					font-family: PingFang SC;
					font-weight: 500;
					color: rgba(150, 150, 150, 1);
				}
			}
		}
	}
</style>
